import { utils } from "./Utils";
import GameItem from "./GameItem";

const {ccclass, property} = cc._decorator;

@ccclass
export default class MoreGamesPanel extends cc.Component {

    panel : cc.Node = null;
    content: cc.Node = null;
    gameItemNode : cc.Node = null;
    
    onLoad(){
        this.panel = this.node.getChildByName("Panel");
        let scrollView : cc.ScrollView = this.panel.getChildByName("ListView").getComponent(cc.ScrollView);
        this.content = scrollView.content;
        this.gameItemNode = this.content.getChildByName("GameItem");

        this.node.active = false;
    }

    onEnable(){
        this.node.on(cc.Node.EventType.TOUCH_START, (event:cc.Event)=>{
            this.hide();
        },this);
    }

    onDisable(){
        this.node.targetOff(this);
    }

    start(){
        this.updatePanel();
    }

    public updatePanel(){
        if(utils.wechatTool && utils.wechatTool.configData && utils.wechatTool.configData.game_list){
            let newItem : cc.Node = null;
            let newGameItem : GameItem = null;
            for(let i=0;i<utils.wechatTool.configData.game_list.length;i++){
                newItem = cc.instantiate(this.gameItemNode);
                newGameItem = newItem.getComponent("GameItem");
                newGameItem.init(utils.wechatTool.configData.game_list[i]);
                this.content.addChild(newItem);
            }

            this.gameItemNode.destroy();
        }
    }

    public show(){
        this.node.active = true;
        this.panel.scale = 0;
        this.panel.runAction(cc.scaleTo(0.3, 1.4).easing(cc.easeBackOut()));
    }

    public hide(){
        let self = this;
        this.panel.runAction(cc.sequence(cc.scaleTo(0.3, 0).easing(cc.easeBackIn()),cc.callFunc(()=>{
            self.node.active = false;
        })));
    }

    public onCloseBtnHandler(event: any, data:any){
        this.hide();
    }
}
